const ctx = new AudioContext();

ctx.resume().catch(console.error);

class Oscillator {
  constructor(frequency, detune) {
    this.osc = ctx.createOscillator();

    this.osc.type = "sawtooth";
    // this.osc.type = "square";
    this.osc.frequency.value = frequency;
    this.osc.detune.value = detune;
    this.osc.connect(ctx.destination);
    this.osc.start(ctx.currentTime);
    this.osc.stop(ctx.currentTime + 3);

    this.osc.addEventListener("ended", () => {
      this.osc.disconnect();
      this.osc = null;
    });
  }
}

window.addEventListener(
  "click",
  () => {
    const o1 = new Oscillator(440, 0);
    const o2 = new Oscillator(440, 15);
    const o3 = new Oscillator(440, 20);
  },
  { once: true }
);
